from time import sleep
import datetime
import re

import requests
from lxml import etree
from pymongo import MongoClient
from selenium.webdriver import Firefox

from freeProxyWebs.proxyweb import ProxyWebs

class XiaohuanProxy(ProxyWebs):
    ##获取小幻代理的代理ip  https://ip.ihuan.me/

    def _get_web(self, url):
        return super()._get_web(url)
    
    def _parser(self,html):
        ##解析网页，返回下一页的url
        tree = etree.HTML(html)
        xpathRowList = '//table/tbody/tr'
        treeRowList = tree.xpath(xpathRowList)
        for treerow in treeRowList:
            treeInfos = treerow.xpath('td')
            ip = treeInfos[0].xpath('a/text()')[0].strip()
            port = treeInfos[1].xpath('text()')[0].strip()
            proxy = {'ip':ip,'port':port,'protocol':'http'}
            self._proxyList.append(proxy)

        nextUrl = self._url +\
             tree.xpath('//a[@aria-label="Next"]/@href')[0] 

        return nextUrl

    def refresh_daily_proxy(self):
        ##更新每日代理
        url = self._url
        for _ in range(10):
            url = self._parser(self._get_web(url))
            sleep(2)

    def init_proxy_pool(self):
        ##初始化代理池
        url = 'https://ip.ihuan.me/ti.html'
        ##使用提取网页提取1000条代理
        with Firefox() as driver:
            driver.get(url)
            sleep(1)
            #保存原始窗口
            originalWindow = driver.current_window_handle

            ##定位输入条数框并输入
            inputNumber = driver.find_element_by_xpath(
                '//input[@name="num"]'
            )
            inputNumber.send_keys('0')
            
            ##定位代理类型并设定高匿代理
            driver.find_element_by_xpath(
                '//select[@name="anonymity"]/option[@value="2"]'
            ).click()

            ##定位排序方式并设定验证时间从近到远
            driver.find_element_by_xpath(
                '//select[@name="sort"]/option[@value="1"]'
            ).click()

            ##点击提取,将打开新窗口
            driver.find_element_by_xpath(
                '//input[@value="提取"]'
            ).click()

            sleep(3)
            #循环执行，找到新窗口句柄
            for window_handle in driver.window_handles:
                if window_handle != originalWindow:
                    driver.switch_to.window(window_handle)
                    break
            
            ##将页面滑动到底
            driver.execute_script(
                "window.scrollTo(0,document.body.scrollHeight)"
            )
            sleep(2)

            html = driver.page_source

        tree = etree.HTML(html)
        proxiesList = tree.xpath(
            '//div[@class="col-md-10"]//div[@class="panel-body"]/text()'
        )
        for proxy in proxiesList:
            proxy = proxy.split(':')
            ip = proxy[0]
            port = proxy[1]
            proxyInfo = {'ip':ip,'port':port,'protocol':'http'}
            self._proxyList.append(proxyInfo)
        
        #将保存的集合初始化
        client = MongoClient('mongodb://localhost:27017/')
        with client:
            db = client.ipPool
            try:
                db.xiaohuan.drop()
            except:
                print('没有集合，直接创建')
            finally:
                db.xiaohuan.insert_many(self._proxyList)
                print("小幻网集合初始化完成")

    def save_proxies(self):
        ##将代理列表保存入数据库
        client = MongoClient('mongodb://localhost:27017/')
        with client:
            db = client.ipPool
            db.xiaohuan.insert_many(self._proxyList)
            print("小幻代理保存完成")
